﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;

namespace Net.DataAccess
{
	public abstract class ArticleProvider : DataAccess
	{
		static private ArticleProvider _instance = null;
		static public ArticleProvider Instance
		{
			get
			{
				if (_instance == null)
					_instance = (ArticleProvider)Activator.CreateInstance(
					   Type.GetType("Net.SqlProvider.ArticleSP"));
				return _instance;
			}
		}

		public ArticleProvider()
		{
			this.ConnectionString = Globals.Settings.ConnectionString;
			this.EnableCaching = Globals.Settings.EnableCaching;
			this.CacheDuration = Globals.Settings.CacheDuration;
		}

		public abstract ArticleDetails FindByID(long id);
		public abstract List<ArticleDetails> FindByTitle(string title);
		public abstract int Insert(ArticleDetails obj);

		protected virtual ArticleDetails GetFromReader(IDataReader reader){
			return new ArticleDetails(
				(long)reader["ID"],
				(DateTime)reader["AddedDate"],
				reader["addedBy"].ToString(),
				reader["title"].ToString(),
				reader["summary"].ToString(),
				reader["body"].ToString(),
				(DateTime)reader["TimePoint"]);
		}
		protected List<ArticleDetails> GetListFromReader(IDataReader reader)
		{
			List<ArticleDetails> items = new List<ArticleDetails>();
			while (reader.Read())
				items.Add(GetFromReader(reader));
			return items;
		}
	}
}